package com.ruoyi.apis.service;

import com.ruoyi.apis.entity.CityEntity;
import com.ruoyi.apis.repository.CityRepository;
import com.ruoyi.apis.vo.CityVO;
import com.ruoyi.apis.vo.DisplayVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Choi
 * @Date 2021/8/23
 */

@Service
public class ICityService {

    @Autowired
    private CityRepository cityRepository;

    @Autowired
    private IDisplayService displayService;

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<CityVO> getCities() {
        List<CityEntity> cityEntities = cityRepository.findAllByOrderBySortAsc();
        if (cityEntities == null) {
            return null;
        }
        List<CityVO> cities = CityVO.getCities(cityEntities);
        int size = cityEntities.size();
        for (int i = 0; i < size; i++) {
            List<DisplayVO> displays = displayService.getDisplays(cities.get(i).getId());
            if (displays != null) {
                cities.get(i).setDisplays(displays);
            }
        }
        return cities;
    }
}
